package cn.edu.stu.syllabus.adapters;

import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.application.SyllabusApplication;
import cn.edu.stu.syllabus.base.BaseQuickClickAdapter;
import cn.edu.stu.syllabus.datas.FunctionItem;
import cn.edu.stu.syllabus.datas.ThemeStyle;
import cn.edu.stu.syllabus.utils.Direction;
import cn.edu.stu.syllabus.utils.DrawableTintUtils;

/**
 * Created by daidaijie on 2017/3/7.
 */

public class FunctionAdapter extends BaseQuickClickAdapter<FunctionItem, FunctionAdapter.ViewHolder> {

    public FunctionAdapter(List<FunctionItem> data) {
        super(R.layout.item_function, data);
    }

    @Override
    protected ViewHolder createBaseViewHolder(View view) {
        return new ViewHolder(view);
    }

    @Override
    protected void convert(final ViewHolder helper, final FunctionItem item) {
        setFunctionToView(item, helper.mFunctionCardView, helper.mFunctionTextView, Direction.TOP);

        helper.mFunctionCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(item, helper.getAdapterPosition());
                }
            }
        });
    }

    public static void setFunctionToView(FunctionItem item, CardView cardView, TextView textView,
                                         @Direction.DirectionInf int direction) {
        textView.setText(item.getName());
        Drawable iconDrawable = DrawableTintUtils.getTintDrawableByColorRes(item.getIcon(),
                ThemeStyle.getCurrentColorPrimaryLight());

        Drawable[] drawables = new Drawable[4];
        drawables[direction - 1] = iconDrawable;
        textView.setCompoundDrawablesWithIntrinsicBounds(
                drawables[0], drawables[1], drawables[2], drawables[3]
        );

        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
            ColorStateList colorStateList = ContextCompat.getColorStateList(
                    SyllabusApplication.getContext(), R.color.selected_card_press
            );
            cardView.setCardBackgroundColor(colorStateList);
            cardView.setForeground(null);
        }

    }

    class ViewHolder extends BaseViewHolder {

        @BindView(R.id.functionCardView)
        CardView mFunctionCardView;
        @BindView(R.id.functionTextView)
        TextView mFunctionTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
